#region Summary

// 
// Owner : JackieHan 
// 
// Solution : LearnSolution 
// 
// Project : JackieHan.NHibernate3.Repository
// 
// File : UserInfoRepository.cs
// 
// CopyRight : Copyright © 2010-2011 Skomart Corporation, All Rights Reserved 
// 
// Summary : UserInfo数据库操作
// 
// History : 
//			2010-09-14 11:59:35	    Create 
// 

#endregion

#region Using

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using JackieHan.NHibernate3.Domain;
using NHibernate;

#endregion

namespace JackieHan.NHibernate3.Repository
{
    /// <summary> 
    /// UserInfo数据库操作
    /// </summary>
    public class UserInfoRepository
    {
        public void Save(UserInfo item)
        {
            NHSessionFactory.EncloseInTransaction(s => s.Save(item));
        }

        /// <summary>
        /// 基本查询
        /// </summary>
        /// <returns></returns>
        public IList<UserInfo> GetAll()
        {
            return NHSessionFactory.SessionFactory.OpenSession().Query<UserInfo>().ToList();
        }

        /// <summary>
        /// 使用Linq查询所有
        /// </summary>
        /// <returns></returns>
        public IList<UserInfo> GetAllByLinq()
        {
            ISession session = NHSessionFactory.SessionFactory.OpenSession();
            return (from user in session.Query<UserInfo>() select user).ToList();
        }

        /// <summary>
        /// API查询使用条件
        /// </summary>
        /// <returns></returns>
        public IList<UserInfo> GetUsersByCondition()
        {
            var query = NHSessionFactory.SessionFactory.OpenSession().Query<UserInfo>();
            query.Where(o => o.Name.Contains("jackie9989"));
            query.Where(o => o.Age > 20);
            return query.ToList();

            
        }

        /// <summary>
        /// Linq查询使用条件
        /// </summary>
        /// <returns></returns>
        public IList<UserInfo> GetUserByConditionWhitLinq()
        {
            ISession session = NHSessionFactory.SessionFactory.OpenSession();
            return (from user in session.Query<UserInfo>()
                    where user.Name.Contains("jackie9989") && user.Age > 20
                    select user).ToList();
        }

        /// <summary>
        /// 查询给定列
        /// </summary>
        /// <returns></returns>
        public ICollection GetUserColumn()
        {
            return NHSessionFactory.SessionFactory.OpenSession()
                .Query<UserInfo>().Select(o => new { o.Name, o.Age }).ToList();
        }

        /// <summary>
        /// 用Linq查询给定列
        /// </summary>
        /// <returns></returns>
        public ICollection GetUserColumnWithLinq()
        {
            var session = NHSessionFactory.SessionFactory.OpenSession();
            var q = from user in session.Query<UserInfo>() select new { user.Age, user.Name };
            return q.ToList();
        }

        /// <summary>
        /// 获取前两行并忽略前两行
        /// </summary>
        /// <remarks>
        /// Take =>对应Top n
        /// Skip =>用RowNumber排序后忽略前面的行数
        /// 单独使用Take或Skip不启作用
        /// </remarks>
        /// <returns></returns>
        public IList<UserInfo> GetUserWithSkipAndTake()
        {
            var session = NHSessionFactory.SessionFactory.OpenSession();
            var q = (from user in session.Query<UserInfo>() select user).Take(2).Skip(2);
            return q.ToList();
        }

        /// <summary>
        /// 获取有序结果集
        /// </summary>
        /// <returns></returns>
        public IList<UserInfo> GetSortedUser()
        {
            var session = NHSessionFactory.SessionFactory.OpenSession();
            var q =
                (from user in session.Query<UserInfo>() orderby user.Id descending, user.Name ascending select user);
            return q.ToList();
        }

        /// <summary>
        /// 使用Group by 查询
        /// </summary>
        /// <returns></returns>
        public ICollection GetByGroup()
        {
            var session = NHSessionFactory.SessionFactory.OpenSession();
            var q =
                (from user in session.Query<UserInfo>()
                 group user by user.Creator
                     into g
                     select new { g.Key, Age = g.Sum(u => u.Age) });
            return q.ToList();
        }
    }
}